package com.cqupt.scanning.system.utils.excel;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 罗倩
 * @Date 2021/10/10 10:41
 */
public class SheetUtils {

    public static Map<String, String> createFieldAndAlias(Class<?> aClass) {
        Map<String, String> fieldAndAlias = new LinkedHashMap<>();
        Field[] fields = aClass.getDeclaredFields();

        for (Field f : fields) {
            if (!f.isAccessible()) {
                // 关闭反射访问安全检查，为了提高速度
                f.setAccessible(true);
            }
            boolean annotationPresent = f.isAnnotationPresent(Cell.class);
            if (annotationPresent) {
                String fieldName = f.getName();
                fieldAndAlias.put(fieldName, f.getAnnotation(Cell.class).alias());
            }
        }


        return fieldAndAlias;
    }


    public static List createFieldAndWeigh(Class<?> aClass) {
        List<Integer> columnWidth = new ArrayList<>();
        Field[] fields = aClass.getDeclaredFields();

        for (Field f : fields) {
            if (!f.isAccessible()) {
                // 关闭反射访问安全检查，为了提高速度
                f.setAccessible(true);
            }
            boolean annotationPresent = f.isAnnotationPresent(Cell.class);
            if (annotationPresent) {
                columnWidth.add(f.getAnnotation(Cell.class).weight());
            }
        }


        return columnWidth;
    }

}
